#!/bin/bash

echo "=== 下载Chart.js静态资源 ==="
echo ""

# 创建静态资源目录
mkdir -p src/main/resources/static/js
mkdir -p src/main/resources/static/css

echo "1. 下载Chart.js核心文件..."
# 下载Chart.js主文件
curl -o src/main/resources/static/js/chart.js https://cdn.jsdelivr.net/npm/chart.js@4.4.0/dist/chart.umd.js

# 下载Chart.js日期适配器
curl -o src/main/resources/static/js/chartjs-adapter-date-fns.js https://cdn.jsdelivr.net/npm/chartjs-adapter-date-fns@3.0.0/dist/chartjs-adapter-date-fns.bundle.min.js

echo "2. 检查下载结果..."
ls -la src/main/resources/static/js/
echo ""

echo "3. 创建本地资源引用文件..."
cat > src/main/resources/static/js/local-chartjs.js << 'EOF'
// 本地Chart.js资源引用
// 如果CDN加载失败，自动回退到本地资源
(function() {
    if (typeof Chart === 'undefined') {
        console.log('CDN Chart.js加载失败，使用本地资源...');
        var script = document.createElement('script');
        script.src = '/static/js/chart.js';
        document.head.appendChild(script);
        
        script.onload = function() {
            var dateAdapterScript = document.createElement('script');
            dateAdapterScript.src = '/static/js/chartjs-adapter-date-fns.js';
            document.head.appendChild(dateAdapterScript);
        };
    }
})();
EOF

echo "4. 下载完成！"
echo "文件位置:"
echo "  - Chart.js: src/main/resources/static/js/chart.js"
echo "  - Date Adapter: src/main/resources/static/js/chartjs-adapter-date-fns.js"
echo "  - 回退脚本: src/main/resources/static/js/local-chartjs.js"
echo ""

echo "=== 下载完成 ===" 